<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="page.brandName"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-input v-model="form.name" style="width: 200px">
        <el-button slot="append" icon="el-icon-circle-plus-outline"  @click="handleAdd"></el-button>
      </el-input>
      <el-button slot="append" icon="el-icon-circle-plus-outline"  @click="formAdd"></el-button>



    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="序号" width="80"></el-table-column>
      <el-table-column prop="number" label="序号" width="80"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="goodBrand" label="推荐"></el-table-column>
      <el-table-column prop="insertTime" label="添加时间"></el-table-column>

      <el-table-column label="是否下架">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.deleted" active-color="#13ce66" inactive-color="#ccc"
                     @change="changeStatus(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>

          </el-popconfirm>
          <el-button type="success" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNum"
          :page-sizes="[2, 4, 8, 10]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="80px" size="small">
        <el-form-item label="分类字段">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="推荐品牌">
          <el-select clearable v-model="form.goodBrand" placeholder="请选择角色" style="width: 80%">
            <el-option v-for="item in brandList" :key="item.brandName" :label="item.brandName" :value="item.brandName"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {sortList, updateSortStatus, deleteSort, addSort, brandList} from "../../api/mallMG.js";

export default {
  name: "Brand",
  data(){
    return {
      brandList: [],
      tableData: [],
      multipleSelection: [],
      page:{
        pageNum: 1,
        pageSize: 4,
      },
      total: 0,
      form: {},
      dialogFormVisible: false,
      dialogImageUrl: '',
      dialogVisible: false,
      myHeaders:{'Access-Control-Allow-Origin':'*'}
    }
  },
  created() {
    this.load()
  },
  methods: {
    formAdd(){
      let page={
        pageNum: 1,
        pageSize: 20,

        brandName: "",
      }
      this.dialogFormVisible = true,
      this.form = {},
      brandList(page).then(res=>{
          this.brandList=res.data.records
      })
    },
    handleAdd(){
      console.log(this.form)
      addSort(this.form).then(res=>{
        this.$message.success("ok")
        this.load()
        this.form={}
      })
      this.dialogFormVisible = false
    },
    load() {
      console.log(this.page)
      sortList(this.page).then(res=>{
        console.log(res)
        if(res.code==200){
          this.tableData=res.data.records
          this.total = res.data.total
        }else{
          this.$message({
            type:'error',
            message:'请求角色列表失败'
          })
        }
      })
    },
    changeStatus(row) {
      row.pageNum = this.page.pageNum,
        row.pageSize = this.page.pageSize

      updateSortStatus(row)
        .then(res => {
          this.$message.success("操作成功")
        if (res.code === '200') {
          this.$message.success("操作成功")
        }
      })
    },
    del(id) {
      console.log(id)
      deleteSort(id).then(res => {
        debugger
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/file/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.page.brandName = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.page.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.page.pageNum = pageNum
      this.load()
    },

    save(){
      addBrand(this.form).then(res=>{
        this.$message.success("添加成功")
      })
      console.log(this.form)
    },
    handleAvatarSuccess(res) {
      this.form.logo = res
      this.$message.success("上传成功")
    },

    uploadImg(param){
      const formData = new FormData()
      formData.append('file', param.file)
      // alert(formData)
      this.$axios.post("/management-mall/leyou.mall/mall-goods/test/oss",formData,{
        'Content-type' : 'multipart/form-data'
      }).then(response => {
        this.form.logo="https://"+response.data
      })
    },

    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
  }
}
</script>

<style>

.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>
